package com.wn.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.common.PageParam;
import com.wn.mapper.DrugMapper;
import com.wn.ov.DrugRecordOv;
import com.wn.ov.HospitalizedApplicationOv;
import com.wn.pojo.Drug;
import com.wn.resopnse.ResponseCode;
import com.wn.resopnse.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.UUID;

/**
 * 药房业务管理
 */
@Service
public class PharmacyService {

    @Autowired
    private DrugMapper drugMapper;

    //查询药品列表
    public ServerResponse getDrugList(PageParam<Drug> pageParam){
        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
                List<Drug> drugList=drugMapper.superSearch(pageParam.getSuperSearchKeyWord());
                PageInfo<Drug> drugPageInfo = new PageInfo<Drug>(drugList);
                return new ServerResponse(ResponseCode.SUCCESS,drugPageInfo);
            }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            List<Drug> drugList = drugMapper.getDrugList();
            PageInfo<Drug> drugPageInfo = new PageInfo<Drug>(drugList);
            return new ServerResponse(ResponseCode.SUCCESS,drugPageInfo);
        }
    }

    //新增药品
    public ServerResponse addDrug(Drug drug){
        drug.setNo(UUID.randomUUID().toString());
        int result = drugMapper.insertSelective(drug);
        if(result > 0){
            //新增成功
            return new ServerResponse(ResponseCode.SUCCESS,"新增成功");
        }
        //新增失败
        return new ServerResponse(ResponseCode.ERROR,"新增失败");
    }

    //修改药品信息
    public ServerResponse updateDrug(Drug drug){
        int result = drugMapper.updateByPrimaryKeySelective(drug);
        if(result > 0){
            //修改成功
            return new ServerResponse(ResponseCode.SUCCESS,"修改成功");
        }
        //修改失败
        return new ServerResponse(ResponseCode.ERROR,"修改失败");
    }

    //删除药品
    public ServerResponse deleteDrug(Integer id){
        int result = drugMapper.updateIsDelete(id);
        if(result > 0){
            //删除成功
            return new ServerResponse(ResponseCode.SUCCESS,"删除成功");
        }
        //删除失败
        return new ServerResponse(ResponseCode.ERROR,"删除失败");
    }

    //使用药品的记录
    public ServerResponse getDrugRecordOvList(PageParam<DrugRecordOv> pageParam){
        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());

            List<DrugRecordOv> drugRecordOvList = drugMapper.superSearch2(pageParam.getSuperSearchKeyWord());
            PageInfo<DrugRecordOv> drugRecordOvPageInfo = new PageInfo<DrugRecordOv>(drugRecordOvList);
            return new ServerResponse(ResponseCode.SUCCESS,drugRecordOvPageInfo);
        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            List<DrugRecordOv> drugRecordOvList = drugMapper.getDrugRecordOvList();
            PageInfo<DrugRecordOv> drugRecordOvPageInfo = new PageInfo<DrugRecordOv>(drugRecordOvList);
            return new ServerResponse(ResponseCode.SUCCESS,drugRecordOvPageInfo);
        }
    }
}
